package com.gxa.jd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.entity.PageResult;
import com.gxa.exception.CustomerException;
import com.gxa.exception.MessageCode;
import com.gxa.jd.mapper.EmployeeMapper;
import com.gxa.jd.pojo.Employee;
import com.gxa.jd.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(String username, String password) throws CustomerException {

        //1.判断空
        if (username == null || username.trim().equals("")) {
            throw new CustomerException(MessageCode.USERNAME_EMPTY);
        }
        if (password == null || password.trim().equals("")) {
            throw new CustomerException(MessageCode.PASSWORD_EMPTY);
        }

        Example example=new Example(Employee.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username);
        example.and(criteria);

        Employee employee = employeeMapper.selectOneByExample(example);

        System.out.println(employee);

        return employee;

    }


    public List<Employee> aaaaa() throws CustomerException {

        List<Employee> employees = employeeMapper.selectAll();

        System.out.println(employees);

        return employees;

    }

    /**
     * 返回全部记录
     *
     * @return
     */
    @Override
    public List<Employee> findAll() {
        return employeeMapper.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    @Override
    public PageResult<Employee> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Employee> employees = (Page<Employee>) employeeMapper.selectAll();
        return new PageResult<Employee>(employees.getTotal(), employees.getResult());
    }

    /**
     * 条件查询
     *
     * @param searchMap 查询条件
     * @return
     */
    @Override
    public List<Employee> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return employeeMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult<Employee> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<Employee> employees = (Page<Employee>) employeeMapper.selectByExample(example);
        return new PageResult<Employee>(employees.getTotal(), employees.getResult());
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    @Override
    public Employee findById(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     *
     * @param employee
     */
    @Override
    public void add(Employee employee) {
        employeeMapper.insert(employee);
    }

    /**
     * 修改
     *
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */

    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Employee.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // name
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // username
            if (searchMap.get("username") != null && !"".equals(searchMap.get("username"))) {
                criteria.andLike("username", "%" + searchMap.get("username") + "%");
            }
            // password
            if (searchMap.get("password") != null && !"".equals(searchMap.get("password"))) {
                criteria.andLike("password", "%" + searchMap.get("password") + "%");
            }
            // salt
            if (searchMap.get("salt") != null && !"".equals(searchMap.get("salt"))) {
                criteria.andLike("salt", "%" + searchMap.get("salt") + "%");
            }
            // token
            if (searchMap.get("token") != null && !"".equals(searchMap.get("token"))) {
                criteria.andLike("token", "%" + searchMap.get("token") + "%");
            }


        }
        return example;
    }

}
